<template>
  <el-dialog :title="info.title" :visible.sync="visible" center width="760px" :before-close="handleCancel" append-to-body>
    <div class="">
      <div class="page-filter mb10 fl">
        <el-form ref="filter" label-width="0">
          <el-form-item class="mr10 mb5 fl" style="width: 300px">
            <el-button v-if="info.type !== 'view'" class="fl" type="primary" size="small" @click="handleAdd">新 增</el-button>
          </el-form-item>
        </el-form>
      </div>
      <el-table
        ref="multipleTable"
        class="search-table"
        :data="list"
        :summary-method="getSummaries"
        show-summary
        border
        fit
        highlight-current-row
        style="width: 100%;"
      >
        <el-table-column label="序号" align="center" width="56px">
          <template slot-scope="{$index}">
            {{ $index + 1 }}
          </template>
        </el-table-column>
        <el-table-column label="付款方式" min-width="260px" prop="value" />
        <el-table-column label="付款比例" align="right" width="140px" :formatter="proportion" />
        <el-table-column v-if="info.type !== 'view'" label="操作" align="center" width="110px" fixed="right">
          <template slot-scope="{row}">
            <el-dropdown style="height:29px">
              <el-button size="mini" type="primary">
                操作<i class="el-icon-arrow-down el-icon--right" />
              </el-button>
              <el-dropdown-menu slot="dropdown">
                <el-dropdown-item @click.native="handleEdit(row)">编 辑</el-dropdown-item>
                <el-dropdown-item @click.native="handleDelete(row)">删 除</el-dropdown-item>
              </el-dropdown-menu>
            </el-dropdown>
          </template>
        </el-table-column>
      </el-table>
      <Dialog ref="dialog" :get-data="getList" :info="dialog" />
    </div>
  </el-dialog>
</template>

<script>
import page from '@/utils/page'
import { proPays, delPays } from '@/api/project'
import Dialog from './paytype-dialog'

export default {
  components: { Dialog },
  extends: page,
  inject: ['app'],
  props: {
    info: {
      type: Object,
      default: null
    },
    getData: {
      type: Function,
      default: function() {}
    },
    reqList: {
      default: proPays,
      type: Function
    }
  },
  data() {
    return {
      total: 0
    }
  },
  watch: {
    visible(val) {
      if (val) {
        this.$set(this, 'list', [])
        this.$set(this, 'form', this.info.edit)
        this.$set(this.filter, 'pro_id', this.form.pro_id)
        this.getList()
        this.$nextTick(() => {
          this.$refs.multipleTable.doLayout()
        })
      }
    },
    getData: {
      type: Function,
      default: function() {}
    }
  },
  mounted() {
  },
  methods: {
    proportion(e) {
      return e.scale + '%'
    },
    getListAfter() {
      this.$set(this, 'total', 0)
      this.list.forEach(e => {
        this.total += e['scale'] || 0
      })
    },
    getSummaries({ columns, data }) {
      const sums = []
      sums[0] = '合计'
      sums[1] = ''
      sums[2] = this.total + '%'
      sums[3] = ''
      return sums
    },
    handleDelete(e) {
      this.$confirm('此操作将删除数据信息，数据将无法恢复, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        delPays({
          ids: e['id']
        }).then(response => {
          this.getList()
          this.$message.success('删除成功')
        })
      })
    },
    handleEdit(e) {
      this.dialog.title = '编辑付款方式'
      this.dialog.type = 'edit'
      e.pro_id = this.form.pro_id
      this.app.dialogParam(this.dialog.edit, ['id', 'pro_id', 'pay_type', 'scale'], e)
      this.$refs['dialog'].visible = true
    },
    handleAdd(e) {
      this.dialog.title = '新增付款方式'
      this.dialog.type = 'add'
      e.pro_id = this.form.pro_id
      this.app.dialogParam(this.dialog.edit, ['id', 'pro_id', 'pay_type', 'scale'], e)
      this.$refs['dialog'].visible = true
    },
    handleCancel(done) {
      this.getData()
      if (this.$refs['formDate']) this.$refs['formDate'].resetFields()
      if (done && typeof done === Function) {
        done()
      } else {
        this.visible = false
      }
    }
  }
}
</script>

<style scoped>
</style>
